#include "studentList.h"

//创建学生信息节点
STUDENT *creat_node_by_basic_information(const char *number, const char *name, const char *phone)
{
    STUDENT *stu = (STUDENT *)malloc(sizeof(STUDENT));

    stu->number = (char *)malloc(strlen(number) + 1);
    stu->name = (char *)malloc(strlen(name) + 1); 
    stu->phone = (char *)malloc(strlen(phone) + 1);
    strcpy(stu->number, number);
    strcpy(stu->name, name);
    strcpy(stu->phone, phone);

    stu->scores = NULL;
    stu->next = NULL;
    return stu;
}
//创建空链表
STUDENT *creat_list()
{
    STUDENT *head = (STUDENT *)malloc(sizeof(STUDENT));
    head->next = NULL;

    return head;
}
// 尾部插入节点
void insert_node(STUDENT *head, STUDENT *node)
{
    if (head == NULL)
    {
        return ;
    }
    STUDENT *index = head;
    while (index->next != NULL)
    {
        index = index->next;
    }

    index->next = node;
}
//根据姓名查找节点
STUDENT *find_node_by_name(STUDENT *head, const char *name)
{
    if (head == NULL)
    {
        return NULL;
    }

    STUDENT *index = head->next;

    while (index != NULL)
    {
        if (strcmp(index->name, name) != 0)
        {
            index = index->next;
        }
        else
        {
            return index;
        }
        
    }

    return NULL;
}
// 删除节点
void del_node(STUDENT **node)
{
    (*node)->next = NULL;
    if ((*node)->scores != NULL)
    {
        free((*node)->scores);
        (*node)->scores = NULL;
    }
    free(*node);
    *node = NULL;
}
// 根据名字删除节点
void del_node_by_name(STUDENT *head, const char *name)
{
    if (head == NULL)
    {
        return ;
    }

    STUDENT *index = head->next;
    STUDENT *index2 = head;
    while (index != NULL)
    {
        if (strcmp(index->name, name) == 0)
        {
            break;
        }
        else
        {
            index2 = index;
            index = index->next;
        }
    }
    if (index == NULL)
    {
        return ;
    }
    index2->next = index->next;
    del_node(&index);
}
// 清空链表
void clear_list(STUDENT *head)
{
    if (head == NULL)
    {
        return ;
    }
    STUDENT *index = head->next;
    while (index != NULL)
    {
        head->next = index->next;
        del_node(&index);
        index = head->next;
    }
}
// 摧毁链表
void destory_list(STUDENT **head)
{
    clear_list(*head);
    free(*head);
    *head = NULL;
}
//打印学生基本信息
void display_basic_information(STUDENT *node)
{
    if (node == NULL)
    {
        return ;
    }
    printf("\t\t%s\t%s\t\t%s ", node->number, node->name, node->phone);
}
//打印学生成绩信息
void display_score(STUDENT *node)
{
    if (node == NULL)
    {
        return ;
    }
    printf("\t\t%s", node->name);
    printf("\t\t%d\t\t%d\t\t%d ", node->scores->Chinese, node->scores->Math, node->scores->English);
}
// 打印全部学生基本信息
void display_all_basic_information(STUDENT *head)
{
    if (head == NULL)
    {
        return ;
    }
    STUDENT *index = head->next;
    while (index != NULL)
    {
       display_basic_information(index);
       printf("\n");
       index = index->next;
    }
    
}
//创建学生成绩节点
SCORES *creat_node_by_score(int Chinese, int Math, int English)
{
    SCORES *score = (SCORES *)malloc(sizeof(SCORES));

    score->Chinese = Chinese;
    score->Math = Math;
    score->English = English;

    return score;
}
// 添加学生成绩
void insert_stuNode_score(STUDENT *head, const char *name, int Chinese, int Math, int English)
{
    STUDENT *node = find_node_by_name(head, name);
    if (node == NULL)
    {
        return ;
    }
    SCORES *score = creat_node_by_score(Chinese, Math, English);
    if (node->scores != NULL)
    {
        free(node->scores);
    }

    node->scores = score;
}
// 学生总数
int sum_node(STUDENT *head)
{
    if (head == NULL || head->next == NULL)
    {
        return 0;
    }
    STUDENT *index = head;
    int count = 0;
    while (index->next != NULL)
    {
        count++;
        index = index->next;
    }
    return count;
}
// 打印所有学生成绩信息
void display_all_scores(STUDENT *head)
{
    if (head == NULL)
    {
        return ;
    }
    STUDENT *index = head->next;
    while (index != NULL && index->scores != NULL)
    {
       display_score(index);
       printf("\n");
       index = index->next;
    }
}
// 修改学生电话号码
void change_phone_number(STUDENT *head, const char *name, const char *NewPhone)
{
    STUDENT *node = find_node_by_name(head, name);
    free(node->phone);
    node->phone = (char *)malloc(strlen(NewPhone) + 1);
    strcpy(node->phone, NewPhone);
}